package com.yangguang.huahui.ui.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ListView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.Area;
import com.yangguang.huahui.listadpter.CommonAdapter;
import com.yangguang.huahui.listadpter.ViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2017/3/21.
 */

public class ProvinceAdapter extends CommonAdapter<Area.ListEntity> {
    private OnItemClickListener mOnItemClickListener;
    private static ListView lv_area;
    private static ListView lv_city;
    private static int proPosition;
    private static int cityPostion;
    private static int areaPosition;

    public ProvinceAdapter(Context context, final List<Area.ListEntity> data, int layoutId, ListView lv_city,
                           ListView lv_area, int proPosition, int cityPostion, int areaPosition) {
        super(context, data, layoutId);
        this.lv_area = lv_area;
        this.lv_city = lv_city;
        this.proPosition = proPosition;
        this.cityPostion = cityPostion;
        this.areaPosition = areaPosition;
    }

    @Override
    public void convert(final ViewHolder holder, final Area.ListEntity listEntity) {
        holder.setText(R.id.tv_content, listEntity.getProvince());
        if (proPosition > -1 && proPosition == holder.getPosition()){
            holder.setBackgroundResource(R.id.tv_content, R.mipmap.choose_item_selected);
            if (mDatas.get(holder.getPosition()).getCity().size() > 0) {
                lv_city.setAdapter(new CityAdapter(mContext, mDatas.get(holder.getPosition()).getCity(), R.layout.select_area_list_item));
                lv_city.setVisibility(View.VISIBLE);
            } else {
                lv_city.setVisibility(View.GONE);
            }
        } else {
            holder.setBackgroundResource(R.id.tv_content, R.color.white);
        }
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notifyDataSetChanged();
                proPosition = holder.getPosition();
                cityPostion = -1;
                areaPosition = -1;
                if (mDatas.get(holder.getPosition()).getCity().size() > 0) {
                    lv_city.setAdapter(new CityAdapter(mContext, mDatas.get(holder.getPosition()).getCity(), R.layout.select_area_list_item));
                    lv_city.setVisibility(View.VISIBLE);
                } else {
                    lv_city.setVisibility(View.GONE);
                }
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(view, holder.getPosition(),
                            mDatas.get(holder.getPosition()).getProvinceid(), mDatas.get(holder.getPosition()).getProvince());
                }
            }
        });
    }

    public static class CityAdapter extends CommonAdapter<Area.ListEntity.CityEntity> {
        private static OnItemClickListener mOnItemClickListener;


        public CityAdapter(Context context, final List<Area.ListEntity.CityEntity> data, int layoutId) {
            super(context, data, layoutId);
        }

        @Override
        public void convert(final ViewHolder holder, final Area.ListEntity.CityEntity cityEntity) {
            holder.setText(R.id.tv_content, cityEntity.getCity());
            if (cityPostion > -1 && cityPostion == holder.getPosition()){
                holder.setBackgroundResource(R.id.tv_content, R.mipmap.choose_item_selected);
                if (mDatas.get(holder.getPosition()).getArea().size() > 0) {
                    lv_area.setAdapter(new AreaAdapter(mContext, mDatas.get(holder.getPosition()).getArea(), R.layout.select_area_list_item));
                    lv_area.setVisibility(View.VISIBLE);
                } else {
                    lv_area.setVisibility(View.GONE);
                }
            } else {
                holder.setBackgroundResource(R.id.tv_content, R.color.white);
            }
            holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    notifyDataSetChanged();
                    cityPostion = holder.getPosition();
                    areaPosition = -1;
                    if (mDatas.get(holder.getPosition()).getArea().size() > 0) {
                        lv_area.setAdapter(new AreaAdapter(mContext, mDatas.get(holder.getPosition()).getArea(), R.layout.select_area_list_item));
                        lv_area.setVisibility(View.VISIBLE);
                    } else {
                        lv_area.setVisibility(View.GONE);
                    }
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(view, holder.getPosition(),
                                mDatas.get(holder.getPosition()).getCityid(), mDatas.get(holder.getPosition()).getCity());
                    }
                }
            });
        }
        public static void setOnItemClickListener(OnItemClickListener l) {
            mOnItemClickListener = l;
        }

        public static class AreaAdapter extends CommonAdapter<Area.ListEntity.CityEntity.AreaEntity> {
            private static OnItemClickListener mOnItemClickListener;

            public AreaAdapter(Context context, final List<Area.ListEntity.CityEntity.AreaEntity> data, int layoutId) {
                super(context, data, layoutId);
            }

            @Override
            public void convert(final ViewHolder holder, final Area.ListEntity.CityEntity.AreaEntity areaEntity) {
                holder.setText(R.id.tv_content, areaEntity.getArea());
                if (areaPosition > -1 && areaPosition == holder.getPosition()){
                    holder.setBackgroundResource(R.id.tv_content, R.mipmap.choose_item_selected);
                } else {
                    holder.setBackgroundResource(R.id.tv_content, R.color.white);
                }
                holder.getConvertView().setOnClickListener( new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        notifyDataSetChanged();
                        areaPosition = holder.getPosition();
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onItemClick(view, holder.getPosition(),
                                    mDatas.get(holder.getPosition()).getAreaid(), mDatas.get(holder.getPosition()).getArea());
                        }
                    }
                });
            }
            public static void setOnItemClickListener(OnItemClickListener l) {
                mOnItemClickListener = l;
            }
        }

    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }
}
